package com.umeox.moto.watch.themes.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;

import com.umeox.moto.watch.themes.R;
import com.umeox.moto.watch.themes.drawable.CircularProgressDrawable;


/**
 * Porting from https://github.com/rey5137/material
 */
public class ProgressView extends View {

    private boolean mAutostart = false;
    private CircularProgressDrawable mProgressDrawable;

    public ProgressView(Context context) {
        super(context);

        init(context, null, 0, 0);
    }

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);

        init(context, attrs, 0, 0);
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        init(context, attrs, defStyleAttr, 0);
    }

    protected void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProgressView, defStyleAttr, defStyleRes);

        int progressId = 0;
        int progressMode = -1;
        float progress = -1;
        float secondaryProgress = -1;

        for (int i = 0, count = a.getIndexCount(); i < count; i++) {
            int attr = a.getIndex(i);

            if (attr == R.styleable.ProgressView_pv_autostart) {
                mAutostart = a.getBoolean(attr, false);
            } else if (attr == R.styleable.ProgressView_pv_progressStyle) {
                progressId = a.getResourceId(attr, 0);
            } else if (attr == R.styleable.ProgressView_pv_progressMode) {
                progressMode = a.getInteger(attr, 0);
            } else if (attr == R.styleable.ProgressView_pv_progress) {
                progress = a.getFloat(attr, 0);
            } else if (attr == R.styleable.ProgressView_pv_secondaryProgress) {
                secondaryProgress = a.getFloat(attr, 0);
            }
        }

        a.recycle();

        mProgressDrawable = new CircularProgressDrawable.Builder(context, progressId).build();

        this.setBackground(mProgressDrawable);

        if (progressMode >= 0) {
            mProgressDrawable.setProgressMode(progressMode);
        }

        if (progress >= 0) {
            setProgress(progress);
        }

        if (secondaryProgress >= 0) {
            setSecondaryProgress(secondaryProgress);
        }
    }

    @Override
    protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
        super.onVisibilityChanged(changedView, visibility);

        if (changedView != this) {
            return;
        }

        if (mAutostart) {
            if (visibility == GONE || visibility == INVISIBLE) {
                stop();
            } else {
                start();
            }
        }
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (getVisibility() == View.VISIBLE && mAutostart) {
            start();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (mAutostart) {
            stop();
        }
        super.onDetachedFromWindow();
    }

    /**
     * @return The current progress of this view in [0..1] range.
     */
    public float getProgress() {
        return mProgressDrawable.getProgress();
    }

    /**
     * Set the current progress of this view.
     *
     * @param percent The progress value in [0..1] range.
     */
    public void setProgress(float percent) {
        mProgressDrawable.setProgress(percent);
    }

    public void setProgressWithAnim(float percent) {
//        mProgressDrawable.setProgress(percent);

        ObjectAnimator animator = ObjectAnimator.ofFloat(mProgressDrawable, "progress", 0, percent * 100);
        animator.setDuration(2000);
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.start();


    }

    /**
     * @return The current secondary progress of this view in [0..1] range.
     */
    public float getSecondaryProgress() {
        return mProgressDrawable.getSecondaryProgress();
    }

    /**
     * Set the current secondary progress of this view.
     *
     * @param percent The progress value in [0..1] range.
     */
    public void setSecondaryProgress(float percent) {
        mProgressDrawable.setSecondaryProgress(percent);
    }

    /**
     * Start showing progress.
     */
    public void start() {
        if (mProgressDrawable != null) {
            mProgressDrawable.start();
        }
    }

    /**
     * Stop showing progress.
     */
    public void stop() {
        if (mProgressDrawable != null) {
            mProgressDrawable.stop();
        }
    }
}
